package it.polito.connectedcar.network;

import it.polito.connectedcar.util.LatLng;
import de.uniluebeck.itm.tcpip.Storage;

public class TrafficLightMessage  extends Message{

	public TrafficLightMessage() {
		super(EntityType.TRAFFIC_LIGHT);
	}
	private float distance;
	private TLPhase nextPhase;
	private LatLng position;
	
	
	public LatLng getPosition()
	{
		return position;
	}
	public float getDistance()
	{
		return distance;
	}

	public TrafficLightMessage.TLPhase  getNetPhase()
	{
		return nextPhase;
	}
	@Override
	protected void convertFromData(Storage st) {
		int code = st.readByte();
		TLColor tlColor = TLColor.fromColorCode(code);
		long elapsedTime = st.readLong();
		this.nextPhase = new TLPhase(tlColor,elapsedTime);
		this.position = new LatLng(st.readDouble(), st.readDouble());
	}
	
	@Override
	protected void convertToData(Storage st) {
		// TODO NOT to implement
		
	}
	
	public class TLPhase
	{
		
		public TLPhase(TLColor color,long elapsedTime) {
			this.elapsedTime= elapsedTime;
			this.actualColor = color;
		}
		private TLColor actualColor;
		private long elapsedTime;
		
		public TLColor getActualColor() {
			return actualColor;
		}
		public void setActualColor(TLColor actualColor) {
			this.actualColor = actualColor;
		}
		public long getElapsedTime() {
			return elapsedTime;
		}
		public void setElapsedTime(long elapsedTime) {
			this.elapsedTime = elapsedTime;
		}
		
	}
	
	public enum TLColor
	{
		RED(0),
		GREEN(1),
		YELLOW(2);
		
		private final int colorCode;
		private TLColor(int colorCode) {
			this.colorCode = colorCode;
		}
		
		
		public int getColorCode()
		{
			return this.colorCode;
		}
		
		public static TLColor fromColorCode(int colorCode)
		{
			for(TLColor tlColor : TLColor.values())
			{
				if(tlColor.getColorCode()==colorCode)
					return tlColor;
			}
			throw new IllegalArgumentException("no TLColor exists for the specified colorCode");
		}
	}
	
}
